package com.chinawlx.wlxteacher.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.ui.fragment.WLXClazzDynamicsFragment;
import com.chinawlx.wlxteacher.ui.fragment.WLXCourseManageFragment;
import com.chinawlx.wlxteacher.ui.fragment.WLXHomeworkCommentFragment;
import com.chinawlx.wlxteacher.ui.fragment.WLXStudentInfoFragment;
import com.chinawlx.wlxteacher.utils.WLXConstant;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener,
        View.OnClickListener {
    @BindView(R.id.rg_content_fragment)
    RadioGroup mRadioGroup;
    @BindView(R.id.rb_course_manage)
    RadioButton course_manage;
    @BindView(R.id.rb_class_dynamics)
    RadioButton class_dynamics;
    @BindView(R.id.rb_job_comment)
    RadioButton job_comment;
    @BindView(R.id.rb_student_information)
    RadioButton student_information;
    @BindView(R.id.iv_back_main)
    ImageView backBtn;
    @BindView(R.id.iv_more_main)
    ImageView moreBtn;
    @BindView(R.id.tv_title_main)
    TextView titleView;
    private FragmentManager mFragmentManager;
    private ArrayList<Fragment> mFragment;
    private List<Integer> buttons = Arrays.asList(R.id.rb_course_manage, R.id.rb_class_dynamics,
            R.id.rb_job_comment, R.id.rb_student_information);
    private PopupWindow mPopupWindow = null;
    private Fragment mCurrentFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
///       requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        init();

    }

    private void init() {
        String title = getIntent().getStringExtra(WLXTeacherConstant.GRADE_TITLE);
        String gradeId = getIntent().getStringExtra(WLXTeacherConstant.GRADE_ID);
        WLXConstant.GRADE_ID = gradeId;
        WLXConstant.GRADE_TITLE = title;
        titleView.setText(title);
        initFragmetns(gradeId);
        initListener();
        mRadioGroup.setOnCheckedChangeListener(this);
        mRadioGroup.check(R.id.rb_course_manage);
    }


    private void initListener() {
        backBtn.setOnClickListener(this);
        moreBtn.setOnClickListener(this);
    }

    private void initFragmetns(String grade_id) {
        Bundle args = new Bundle();
        args.putString(WLXTeacherConstant.GRADE_ID, grade_id);
        mFragment = new ArrayList<>();
        WLXCourseManageFragment wlxCourseManageFragment = new WLXCourseManageFragment();
        WLXClazzDynamicsFragment wlxClazzDynamicsFragment = new WLXClazzDynamicsFragment();
        WLXHomeworkCommentFragment wlxHomeworkCommentFragment = new WLXHomeworkCommentFragment();
        WLXStudentInfoFragment wlxStudentInfoFragment = new WLXStudentInfoFragment();
        wlxStudentInfoFragment.setArguments(args);
        wlxCourseManageFragment.setArguments(args);
        wlxClazzDynamicsFragment.setArguments(args);
        wlxHomeworkCommentFragment.setArguments(args);
        mFragment.add(wlxCourseManageFragment);
        mFragment.add(wlxClazzDynamicsFragment);
        mFragment.add(wlxHomeworkCommentFragment);
        mFragment.add(wlxStudentInfoFragment);
        setDefaultFragment(mFragment.get(0));
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        } else {
            int position = buttons.indexOf(checkedId);
            switchFragment(mFragment.get(position));
        }


    }

    /**
     * 设置默认的fragment
     *
     * @param fm
     */
    private void setDefaultFragment(Fragment fm) {
        mFragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.add(R.id.fl_container, fm).commit();
        mCurrentFragment = fm;
    }

//    /**
//     * 切换fragment,会重新实例化
//     *
//     * @param position
//     */
//    private void switchFragment(int position) {
//        FragmentTransaction transaction = mFragmentManager.beginTransaction();
//        mFragmentManager.popBackStack();
//        transaction.replace(R.id.fl_container, mFragment.get(position));
//        transaction.commit();
//    }

    /**
     * 切换fragment,不会重新实例化
     *
     * @param to
     */
    private void switchFragment(Fragment to) {
        if (mCurrentFragment != to) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            // 先判断是否被add过
            if (!to.isAdded()) {
                // 隐藏当前的fragment，add下一个到Activity中
                transaction.hide(mCurrentFragment).add(R.id.fl_container, to).commit();
            } else {
                // 隐藏当前的fragment，显示下一个
                transaction.hide(mCurrentFragment).show(to).commit();
            }
            mCurrentFragment = to;
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back_main:
                finish();
                break;
            case R.id.iv_more_main:
                if (mPopupWindow == null) {
                    showPopupwindow();
                } else {
                    mPopupWindow.dismiss();
                    mPopupWindow = null;
                }
                break;
        }
    }

    private void showPopupwindow() {
        View popView = LayoutInflater.from(this).inflate(R.layout.popupwindow_more, null);
        mPopupWindow = new PopupWindow();
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setContentView(popView);
        // 允许点击外部消失,部分机型无效
        mPopupWindow.setOutsideTouchable(true);
        //新方法,点击外部消失.
        mPopupWindow.setFocusable(false);
        int moreBtnX = (int) moreBtn.getX();
        int moreBtnY = (int) moreBtn.getY();
        mPopupWindow.showAsDropDown(moreBtn, moreBtnX / 2, moreBtnY);
        mPopupWindow.update();
        popView.findViewById(R.id.tv_leave_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ClazzLeaveActivity.class));
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        });
        popView.findViewById(R.id.tv_stop_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, WLXClazzSuspendActivity.class));
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        });
        popView.findViewById(R.id.tv_adjust_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, WLXClazzAdjustActivity.class));
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        });
        popView.findViewById(R.id.tv_add_pop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, WLXClazzAddActivity.class));
                mPopupWindow.dismiss();
                mPopupWindow = null;
            }
        });

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
            mPopupWindow = null;
        }
        return super.onTouchEvent(event);
    }
}
